package com.beilang.hotline.person.manager.impl;

import java.util.ArrayList;
import java.util.List;

import com.beilang.base.codelookup.manager.CodeLookupMgr;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.CodeConstant;
import com.beilang.hotline.person.dao.PersonDAO;
import com.beilang.hotline.person.manager.PersonMgr;
import com.beilang.hotline.person.model.PersonModel;
import com.beilang.hotline.person.to.SearchTO;

@SuppressWarnings("unchecked")
public class PersonMgrImpl implements PersonMgr {
	private PersonDAO personDAO;
	
	private CodeLookupMgr codeLookupMgr;
	
	public void setCodeLookupMgr(CodeLookupMgr codeLookupMgr) {
		this.codeLookupMgr = codeLookupMgr;
	}
	
	public void setPersonDAO(PersonDAO personDAO) {
		this.personDAO = personDAO;
	}

	public PersonModel createPerson(PersonModel personModel) {
		return personDAO.createPerson(personModel);
	}

	public PersonModel savePerson(PersonModel personModel) {
		return personDAO.savePerson(personModel);
	}

	public void deletePerson(Long personId) {
		personDAO.deletePerson(personId);
	}

	public PersonModel getPerson(Long personId) {
		PersonModel personModel = personDAO.getPerson(personId);
        if (personModel!=null)
        {
            personModel.setCompanyName(codeLookupMgr.getCodeDesc(CodeConstant.COMPANY_NAME, String.valueOf(personModel.getCompanyId())));
            personModel.setSexDesc(codeLookupMgr.getCodeDesc(CodeConstant.CODE_SEX_LIST, personModel.getSex()));
        }
        return personModel;
	}

	public PaginationHelper getPersonList(PaginationHelper helper, SearchTO searchTO) {
		int count = personDAO.searchCount(searchTO);
		helper.setTotalSize(count);
		List list = personDAO.searchList(helper, searchTO);
		if(list!=null&&list.size()>0){
			List l=new ArrayList();
            for (Object aList : list) {
                PersonModel personModel = (PersonModel) aList;
                personModel.setCompanyName(codeLookupMgr.getCodeDesc(CodeConstant.COMPANY_NAME, String.valueOf(personModel.getCompanyId())));
                personModel.setSexDesc(codeLookupMgr.getCodeDesc(CodeConstant.CODE_SEX_LIST, personModel.getSex()));
                l.add(personModel);
            }
			helper.setList(l);
		}else{
			helper.setList(list);
		}
		return helper;
	}

    public void deleteAllCompanyPersons(Long companyId){
        personDAO.deleteAllCompanyPersons(companyId);
    }

    public List getPersonList(String personName, String telNo, String handPhone){
        return personDAO.getPersonList(personName,telNo,handPhone);
    }

}
